//
//  BookOrderDetailViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/4/29.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "BookOrderDetailViewController.h"
#import "BookOrderDetailCell.h"
#import "BookOrderDetailHeaderCell.h"
#import "PayBookOrderSuccessController.h"
#import "BookOrderDetailFooterCell.h"
#import "SVProgressHUD.h"
#import "HttpClientManager.h"
#import "AppDelegate.h"
#import "AppMacro.h"
#import "MyOrderViewController.h"
#import "ParkAnnotationView.h"

@interface BookOrderDetailViewController ()<UITableViewDataSource, UITableViewDelegate, BookOrderDetailFooterCellDelegate, UIAlertViewDelegate, UIActionSheetDelegate>

@end

@implementation BookOrderDetailViewController{
    
    PayBookOrderSuccessController *payBookOrderSuccessController;
    
    LoginModel *loginModel;
    
    BookingOrderDetailModel *orderDetail;
    NSInteger totalRows;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    loginModel = ApplicationDelegate.loginModel;
    self.orderType = ORDERTYPE_BOOK_CODE;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    [self requestOrderDetail];
}

#pragma mark - UITableView Delegate && DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return totalRows;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger row = indexPath.row;
    if(row == 0){
        
        return 70;
    }else if(row == totalRows - 1){
        
        return 212;
    }
    return 56;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *orderCellIdentifier = @"BookOrderDetailCell";
    
    NSInteger row = indexPath.row;
    
    if(row == 0){
        
        BookOrderDetailHeaderCell *headerCell = [[[NSBundle mainBundle] loadNibNamed:@"BookOrderDetailHeaderCell" owner:self options:nil] lastObject];
        [headerCell initDataWithBookingOrderModel:orderDetail];
        return headerCell;
    }else if(row == totalRows - 1){
        
        BookOrderDetailFooterCell *footerCell = [[[NSBundle mainBundle] loadNibNamed:@"BookOrderDetailFooterCell" owner:self options:nil] lastObject];
        [footerCell initDataWithBookingOrderModel:orderDetail];
        footerCell.delegate = self;
        return footerCell;
    }else{
        
        SubBookingOrderModel *subBookingOrderModel = orderDetail.subBookingOrderList[row - 1];
        
        BookOrderDetailCell *cell = [self.tableView dequeueReusableCellWithIdentifier:orderCellIdentifier];
        [cell initDataWithSubBookingOrderModel:subBookingOrderModel];
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - BookOrderDetailFooterViewDelegate
- (void)cancelOrderAction{
    
    //取消订单
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"酷位停车提示" message:@"确认取消订单" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alertView show];
}

- (void)payOrderAction{
    
    //订单支付
    [self payBookOrderAction];
}

- (void)showShareView{
    
    payBookOrderSuccessController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PayBookOrderSuccessController"];
    [self.view.window addSubview:payBookOrderSuccessController.view];
}

#pragma mark - IBActions

- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)guideAction:(id)sender {
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"开启至停车场导航",@"查看停车场室内路线图", nil];
    [actionSheet showInView:self.view];
}

#pragma mark - ActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex == 0){
        
        [self guideToParkingPort];
    }else if(buttonIndex == 1){
        
        [self guidInnerParkingPort];
    }
}

- (void)guideToParkingPort{
    
    //导航到停车场
    ParkingSearchModel *model = [[ParkingSearchModel alloc] init];
    model.gpsLat = @"31.273025";
    model.gpsLon = @"121.431199";
    ParkAnnotationView *parkAnnotationView = [[ParkAnnotationView alloc] init];
    parkAnnotationView.parkingSearchModel = model;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:Notify_ShowGuideToParkPort object:nil userInfo:@{@"ParkAnnotationView": parkAnnotationView}];
    
    NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
    for(UIViewController *vC in self.navigationController.viewControllers){
        
        if([vC isKindOfClass:[self class]]){
            
            [viewControllers removeObject:vC];
            continue;
        }
        
        if([vC isKindOfClass:[MyOrderViewController class]]){
            
            [viewControllers removeObject:vC];
            continue;
        }
    }
    self.navigationController.viewControllers = viewControllers;
}

- (void)guidInnerParkingPort{
    
    //室内路线图
}

#pragma mark - 订单详情

- (void)requestOrderDetail{
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] orderDetailRequestWithOrderno:self.orderNo WithUserId:loginModel.userId WithOrdertype:self.orderType WithSucessBlock:^(JsonRetMsg *response) {
        
        [SVProgressHUD dismiss];
        
        orderDetail = [[BookingOrderDetailModel alloc] initWithDictionary:response.data error:nil];
        totalRows = orderDetail.subBookingOrderList.count + 2;
        [self.tableView reloadData];
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}


#pragma mark - 取消预约订单
- (void)requestCancelBookOrder{
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] cancelBookOrderRequestWithOrderno:self.orderNo WithUserId:loginModel.userId WithMobilePhone:loginModel.mobilePhone WithSucessBlock:^(JsonRetMsg *response) {
        
        [SVProgressHUD dismiss];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD showSuccessWithStatus:@"订单已取消"];
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}

#pragma mark - 订单支付
- (void)payBookOrderAction{
    
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeClear];
    [[HttpClientManager sharedClient] payBookOrderRequestWithOrderno:self.orderNo WithUserId:loginModel.userId WithMobilePhone:loginModel.mobilePhone WithSucessBlock:^(JsonRetMsg *response) {
        
        [SVProgressHUD dismiss];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD showSuccessWithStatus:@"订单支付成功"];
            [self showShareView];
        });
        
    } withFailedBlock:^(JsonRetMsg *responseObject) {
        
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(buttonIndex == 1){
        
        [self requestCancelBookOrder];
    }
}

@end
